import 'package:flutter/material.dart';
import '../../models/message_model.dart';
import 'dart:convert';
import '../../l10n/app_localizations.dart';

class AreaCodePickView extends StatefulWidget {
  final Function(AreaCodeModel) onAreaCodeSelected;
  
  const AreaCodePickView({Key? key, required this.onAreaCodeSelected}) : super(key: key);

  @override
  _AreaCodePickViewState createState() => _AreaCodePickViewState();
}

class _AreaCodePickViewState extends State<AreaCodePickView> {
  List<AreaCodeModel> areaArray = [];
  List<AreaCodeModel> newArray = [];
  final TextEditingController _searchController = TextEditingController();
  
  @override
  void initState() {
    super.initState();
    _loadAreaCodeData();
    _searchController.addListener(_onSearchChanged);
  }

  Future<void> _loadAreaCodeData() async {
    // 加载地区数据
    final String data = await DefaultAssetBundle.of(context).loadString('assets/json/area.json');
    final List<dynamic> jsonList = json.decode(data);
    
    setState(() {
      areaArray = jsonList.map((e) => AreaCodeModel.fromJson(e)).toList();
      newArray = areaArray;
    });
  }

  void _onSearchChanged() {
    final query = _searchController.text;
    setState(() {
      if (query.isEmpty) {
        newArray = areaArray;
      } else {
        newArray = areaArray.where((item) => 
          item.name.toLowerCase().contains(query.toLowerCase())
        ).toList();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 250,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(12)),
      ),
      child: Column(
        children: [
          // 工具栏
          Container(
            height: 50,
            padding: EdgeInsets.symmetric(horizontal: 16),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.vertical(top: Radius.circular(12)),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                TextButton(
                  onPressed: () => Navigator.pop(context),
                  child: Text(AppLocalizations.of(context)!.cancel),
                ),
                Container(
                  width: 130,
                  height: 30,
                  decoration: BoxDecoration(
                    color: Color(0xFFF3F7FF),
                    borderRadius: BorderRadius.circular(15),
                    border: Border.all(color: Color(0xFFD2D2D2)),
                  ),
                  padding: EdgeInsets.only(left: 15),
                  child: TextField(
                    controller: _searchController,
                    decoration: InputDecoration(
                      hintText: AppLocalizations.of(context)!.enterTheAreaCode,
                      border: InputBorder.none,
                      contentPadding: EdgeInsets.only(bottom: 10),
                    ),
                    keyboardType: TextInputType.number,
                  ),
                ),
              ],
            ),
          ),
          // 选择器
         Expanded(
            child: ListView.builder(
              itemCount: newArray.length,
              itemBuilder: (context, index) {
                final item = newArray[index];
                return InkWell(
                  onTap: () {
                    widget.onAreaCodeSelected(item);
                    Navigator.pop(context);
                  },
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 8), // 你想要的间距
                    child: Text(item.label),
                  ),
                );
              },
            ),
          )

        ],
      ),
    );
  }

  @override
  void dispose() {
    _searchController.dispose();
    super.dispose();
  }
}